﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using MyLib;
 

namespace PerformanceReport
{
    public partial class ProcessDosage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBindProcess();
                //BindData();               
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 
            }
        }

        //绑定下拉框
        public void DataBindProcess()
        {
            DataTable dtProcess;
            string strSql = "select Distinct ProcessName from PROCESSES ";
            dtProcess = DataAccess.Instance("UltDB").ExecuteDataTable(strSql.ToString());
            drpBusinessUnit.DataSource = dtProcess;

            drpBusinessUnit.DataTextField = "ProcessName";

            drpBusinessUnit.DataValueField = "ProcessName";

            drpBusinessUnit.DataBind();

            drpBusinessUnit.Items.Insert(0, new ListItem("--请选择--", "0"));

        }


        private void BindData()
        {
            try
            {
                DataTable dt;
                int PageIndex = AspNetPager1.CurrentPageIndex - 1;
                int PageSize = AspNetPager1.PageSize;

                string strSql = "select  ProcessName,Count(INCIDENT) as INCIDENT,"
                + "SUM(case when status=2 then 1 else 0 end) as Completed,SUM(case when status=1 then 1 else 0 end) as UnFinished"
                + ",SUM(case when status=4 then 1 else 0 end) as Cancelled"
                + "  from  Incidents ";
                string where = " where 1=1 ";
                if (!string.IsNullOrEmpty(txtStart.Text))
                {
                    where += " and  starttime >= '" + txtStart.Text.Trim() + "'";

                }
                if (!string.IsNullOrEmpty(txtEnd.Text))
                {                    
                    where += " and  starttime <= '" + txtEnd.Text.Trim() + "'";

                }                
                where += "and ProcessName like N'%" + drpBusinessUnit.SelectedValue.Trim() + "%'";
                where += " group by ProcessName";
                 
                string StrSqlTotal = strSql + where;
                dt = DataAccess.Instance("UltDB").ExecuteDataTable(StrSqlTotal.ToString());

                //初始化分页数据源实例
                PagedDataSource pds = new PagedDataSource();
                //设置总行数
                AspNetPager1.RecordCount = dt.Rows.Count;
                //设置分页的数据源
                pds.DataSource = dt.DefaultView;
                //设置当前页
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                //设置每页显示页数
                pds.PageSize = AspNetPager1.PageSize;
                //启用分页
                pds.AllowPaging = true;

                //设置Repeater的数据源为分页数据源
                rpSource.DataSource = pds;
                rpSource.DataBind();

                lbTotal.Text = "共" + dt.Rows.Count.ToString() + "条记录";

            }
            catch (Exception ex)
            {
                MessageBox("alert('" + ex.Message + "');");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //AspNetPager1.CurrentPageIndex = e.;

            BindData();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 

        }

        //完成率计算
        public string GetCompletionRate(string Completed, string INCIDENT)
        {
            //string CompletionRate = Convert.ToString(Math.Round(Convert.ToDecimal(Completed) / Convert.ToDecimal(INCIDENT), 0)) + "%";
            double com = double.Parse(Completed);
            double inc = double.Parse(INCIDENT);
            //double result = double.Parse((com / inc).ToString("0.0"));
            double result = Math.Round((com / inc), 3, MidpointRounding.AwayFromZero);  
            // MidpointRounding.AwayFromZero
            string CompletionRate = result.ToString("P");
            return CompletionRate;
        }

        //平均完成率
        public string GetAvgCompletionRate(string Completed, string INCIDENT, string Cancelled)
        {           
            double com = double.Parse(Completed);
            double inc = double.Parse(INCIDENT);
            double can = double.Parse(Cancelled);
            //double result = double.Parse((com / inc).ToString("0.0"));
            double result = Math.Round((com / (inc-can)), 3, MidpointRounding.AwayFromZero);
        
            string CompletionRate = result.ToString("P");
            return CompletionRate;
        }

        private void MessageBox(string script)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "javascript", "<script language='javascript' defer>" + script + "</script>");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "onload", "document.getElementById('container').style.display='none'", true); 
            
        }
    }
}